package com.example.service;

import com.example.dto.MessageDto;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.FilterEvent;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import java.net.InetSocketAddress;

@Service
public class ServerServiceImpl implements ServerService, InitializingBean, CommandLineRunner, DisposableBean {

    Logger logger = LoggerFactory.getLogger(getClass());

    private IoAcceptor acceptor;

    @Override
    public void afterPropertiesSet() throws Exception {
        this.acceptor = new NioSocketAcceptor();
        this.acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
        this.acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
        this.acceptor.setHandler(new IoHandler() {
            @Override
            public void sessionCreated(IoSession ioSession) throws Exception {

            }

            @Override
            public void sessionOpened(IoSession ioSession) throws Exception {
            }

            @Override
            public void sessionClosed(IoSession ioSession) throws Exception {

            }

            @Override
            public void sessionIdle(IoSession ioSession, IdleStatus idleStatus) throws Exception {

            }

            @Override
            public void exceptionCaught(IoSession ioSession, Throwable throwable) throws Exception {

            }

            @Override
            public void messageReceived(IoSession ioSession, Object o) throws Exception {
                if (o instanceof MessageDto) {
                    logger.warn("server收到信息:" + o.toString());
                }
                ioSession.write(o);
            }

            @Override
            public void messageSent(IoSession ioSession, Object o) throws Exception {

            }

            @Override
            public void inputClosed(IoSession ioSession) throws Exception {

            }

            @Override
            public void event(IoSession ioSession, FilterEvent filterEvent) throws Exception {

            }
        });
    }

    @Override
    public void run(String... args) throws Exception {
        this.acceptor.bind(new InetSocketAddress(8888));
        logger.warn("acceptor started " + 8888);
    }

    @Override
    public void destroy() throws Exception {
        this.acceptor.dispose();
    }

    @Override
    public IoAcceptor getAcceptor() {
        return this.acceptor;
    }
}
